<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Booking extends Base
{
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
    }
    public function lists(){
        return view('booking/lists');
    }
   public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $sid= isset($input['sid'])?intval($input['sid']):0;
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $where = [];
        $where[] = ['sid','=',$sid];
        $where[] = ['status','=',0];
        $field = '*';
        $count=Db::name('appointment')->where($where)->count();
        $list  = Db::name('appointment')
              ->field($field)
              ->where($where)
              ->order('id desc')
              ->limit($page_start, $page_limit)->select();
        foreach($list as $k =>$v){
          $list[$k]['create_date'] = date('Y-m-d H:i',$v['createtime']);
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['bookings'=>$list];
        return $this->print_result($this->res_data);
   }
  public function updateBooking($param)
  {
    $aid = isset($param['aid']) ? intval($param['aid']):0;
    $sid = isset($param['sid']) ? intval($param['sid']):0;
    $status = isset($param['status']) ? intval($param['status']):0;
    if (!$aid || !$sid) {
      $this->res_data['Msg'] = "缺少参数";
      return $this->res_data;
    }
    $booking = Db::name('appointment')->where([['id','=',$aid],['sid','=',$sid]])->find();
    if (!$booking) {
      $this->res_data['Msg'] = "无此预约信息";
      return $this->res_data;
    }
    $data['status'] =$status;
    $data['updatetime'] =time();
    $tdata = Db::name('appointment')->where([['id','=',$tid]])->data($data)->update();
    if(!$tdata) {
        $this->res_data['Msg'] = '更新失败';
        return $this->res_data;
    }
    $this->res_data['Msg'] = '成功';
    $this->res_data['Code'] = 0;
    return $this->printResult($this->res_data);
  }
}
